/*
 * Author: nicolas.bredeche(@)lri.fr
 * Created on 7 janv. 2006
 * 
 */

package picoevo.tutorials.simbad.robotavoidbehavior;

import picoevo.core.evolution.ParameterSet;
import picoevo.core.representation.World;
import picoevo.ext.evolution.InitialisationOperator_Element_StaticArray_Double;
import picoevo.ext.evolution.InitialisationOperator_Individual_StaticArray_Double;
import picoevo.ext.evolution.SelectionOperator_MuLambda;
import picoevo.ext.evolution.StatisticsOperator_Population;
import picoevo.ext.representation.Population_SimplePopulation;
import picoevo.toolbox.Dictionary;
import picoevo.toolbox.Display;
import picoevo.toolbox.Misc;
import picoevo.toolbox.Version;

public class Main_SimpleAvoiderRobot {
	public static void launchEvolution() {
		Display.info("#### Simple Avoider Robot - Optimising weights ####");

		// 1 - create an Evolution environment along with a single embedded
		// Population

		ParameterSet parameterSet = new ParameterSet();

		parameterSet.setProperty("GenomeSize", 30);
		parameterSet.setProperty("Mu", 2);
		parameterSet.setProperty("Lambda", 18);
		parameterSet.setProperty("MutationRate", 0.9);
		parameterSet.setProperty("MuPlusLambda", true);
		parameterSet.setProperty("Generations", 250);
		parameterSet.setProperty("InitPopSize", 20);
		parameterSet.setProperty("OptimisationFlag", Boolean.toString(Dictionary.Maximisation));

		parameterSet.setProperty("SelectionOperator", new SelectionOperator_MuLambda("MuLambda"));
		parameterSet.setProperty("EvaluationOperator_Individual", new EvaluationOperator_SimpleAvoiderRobot("avoiderRobot"));
		parameterSet.setProperty("PopulationInitialisationOperator", new InitialisationOperator_Population_SimpleAvoiderRobot("avoiderRobot"));
		parameterSet.setProperty("IndividualInitialisationOperator", new InitialisationOperator_Individual_StaticArray_Double("avoiderRobot"));
		parameterSet.setProperty("ElementInitialisationOperator", new InitialisationOperator_Element_StaticArray_Double(-1, 1));
		parameterSet.setProperty("PopulationStatisticsOperator", new StatisticsOperator_Population());

		parameterSet.setProperty("LogFilename", "log/logfile_simpleAvoiderRobot_" + Misc.getCurrentTimeAsCompactString() + ".data");
		parameterSet.check();
		parameterSet.displayInformation();

		Display.info("\n\n## initialization ##");

		World myWorld = new World("myEvolution", parameterSet);
		Population_SimplePopulation population = new Population_SimplePopulation("max-one population", myWorld);
		myWorld.registerPopulation(population);

		population.performInitialisation();

		// maxOnePop.displayInformation();

		// 2 - evolution

		Display.info("\n\n## evolving ##");

		int displayFreq = 10;

		for (int i = 0; i != myWorld.getTemplate().getIntProperty("Generations"); i++) {
			if (i % displayFreq == 0)
				myWorld.evolveOneStep(true);
			else
				myWorld.evolveOneStep(false);
		}

		// maxOnePop.displayInformation();

		Display.info("\n\n\n## terminating ##\n");
	}

	public static void main(String[] args) {
		double startTime = System.currentTimeMillis();

		Version.displayCurrentReleaseInformation();

		Display.info("###PicoEvo Tutorial Example###");
		Display.info("Running...");
		launchEvolution();
		Display.info("\nTerminated (" + ((System.currentTimeMillis() - startTime) / 1000) + "s elapsed).");
	}

}
